import request from "@/utils/request";
import Cookies from "js-cookie";
import global_ from "../global.config";
import wx from "weixin-js-sdk";
import Qs from "qs";

function _shareScene(sceneType) {
  let query = Qs.parse(location.search.substring(1));
  let baseUrl = `${location.protocol}//${location.host}${location.pathname}`;
  let url = "";
  if (!global_.SHARE_LINK) {
    if (query.j) {
      delete query.j;
    }
    if (query.cmd) {
      delete query.cmd;
    }
    if (query.fake) {
      delete query.fake;
    }
    url = Qs.stringify(query) ? `${baseUrl}?${Qs.stringify(query)}` : baseUrl;
  } else {
    url = global_.SHARE_LINK;
  }
  // return encodeURIComponent(url);
  // let str = Qs.stringify({
  //   t: "share",
  //   preid: Cookies.get("openid") || "",
  //   appid: global_.APPID,
  //   // url: encodeURIComponent(url),
  //   scene: sceneType
  // });
  // url = url.indexOf("?") >= 0 ? `${url}&${str}` : `${url}?${str}`;
  url = global_.SHARE_API.replace("{desturl}",encodeURIComponent(url));
  return url;
  // return url.indexOf("?") >= 0 ? `${url}&${str}` : `${url}?${str}`;
}

function _shareCallback_OK(scene) {
    global_.SHARE_CALLBACK_OK(scene);
}

export default {
  init: function(callback) {
    request
      .get(global_.API_URL, {
        action: "getAppid"
      })
      .then(response => {
        this.wxInit(response.jsParams);
        if (callback) callback();
      });
  },
  wxInit(jsParams) {
    wx.config({
      debug: false,
      appId: global_.APPID,
      timestamp: jsParams.timestamp,
      nonceStr: jsParams.noncestr,
      signature: jsParams.signature,
      jsApiList: [
        "checkJsApi",
        "updateAppMessageShareData",
        "updateTimelineShareData",
        "onMenuShareTimeline",
        "onMenuShareAppMessage",
        "onMenuShareQQ",
        "onMenuShareWeibo",
        "hideMenuItems",
        "showMenuItems",
        "hideAllNonBaseMenuItem",
        "showAllNonBaseMenuItem",
        "translateVoice",
        "startRecord",
        "stopRecord",
        "onRecordEnd",
        "playVoice",
        "pauseVoice",
        "stopVoice",
        "uploadVoice",
        "downloadVoice",
        "chooseImage",
        "previewImage",
        "uploadImage",
        "downloadImage",
        "getNetworkType",
        "openLocation",
        "getLocation",
        "hideOptionMenu",
        "showOptionMenu",
        "closeWindow",
        "scanQRCode",
        "chooseWXPay",
        "openProductSpecificView",
        "addCard",
        "chooseCard",
        "openCard"
      ]
    });
    wx.ready(function() {
      //自定义“分享给朋友”及“分享到QQ”按钮的分享内容
      // wx.updateAppMessageShareData({
      wx.onMenuShareAppMessage({
        title: global_.SHARE_TITLE,
        desc: global_.SHARE_DESC,
        link: _shareScene(1),
        imgUrl: global_.SHARE_IMG_URL,
        success: function(res) {
            _shareCallback_OK(1);
        },
        cancel: function(res) {}
      });
      //自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
      // wx.updateTimelineShareData({
      wx.onMenuShareTimeline({
        title: global_.SHARE_TIMELINE_TITLE
          ? global_.SHARE_TIMELINE_TITLE
          : global_.SHARE_TITLE,
        link: _shareScene(2),
        imgUrl: global_.SHARE_IMG_URL,
        success: function(res) {
            _shareCallback_OK(2);
        },
        cancel: function(res) {}
      });
      wx.onMenuShareQQ({
        title: global_.SHARE_TITLE,
        desc: global_.SHARE_DESC,
        link: _shareScene(3),
        imgUrl: global_.SHARE_IMG_URL,
        success: function(res) {
            _shareCallback_OK(3);
        },
        cancel: function(res) {}
      });
      wx.onMenuShareWeibo({
        title: global_.SHARE_TITLE,
        desc: global_.SHARE_DESC,
        link: _shareScene(4),
        imgUrl: global_.SHARE_IMG_URL,
        success: function(res) {
            _shareCallback_OK(4);
        },
        cancel: function(res) {}
      });
      wx.onMenuShareQZone({
        title: global_.SHARE_TITLE,
        desc: global_.SHARE_DESC,
        link: _shareScene(5),
        imgUrl: global_.SHARE_IMG_URL,
        success: function(res) {
            _shareCallback_OK(5);
        },
        cancel: function(res) {}
      });
    });
  }
};
